package com.ktjy.springsecuritydemo.handle;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

public class MyAuthentictionSuccessHandler implements AuthenticationSuccessHandler {

    private final String url;
    public MyAuthentictionSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication) throws IOException, ServletException {
        User user = (User) authentication.getPrincipal();
        System.out.println("名字:"+user.getUsername());
        System.out.println("密码："+user.getPassword());
        System.out.println("权限列表:" + user.getAuthorities());

        response.sendRedirect(url);
    }
}
